package com.windliven.spoc.modules.salary.salarymanagement.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.windliven.spoc.modules.salary.salarymanagement.pojo.entity.User;
import com.windliven.spoc.modules.salary.salarymanagement.pojo.vo.ResultVo;
import com.windliven.spoc.modules.salary.salarymanagement.service.UserService;
import com.windliven.spoc.modules.salary.salarymanagement.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author fly
 * @version V1.0
 * @date 2018/11/14
 */
@Slf4j
@RestController
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("/addUser")
    public ResultVo addUser(User user) {
        user.setUpdateTime(new Date());
        userService.addUser(user);
        return ResultUtil.success();
    }

    @GetMapping("/updateUser")
    public ResultVo updateUser(User user) {
        user.setUpdateTime(new Date());
        userService.updateUser(user);
        return ResultUtil.success();
    }

    @GetMapping("/delUser")
    public ResultVo delUser(User user) {
        userService.delUser(user);
        return ResultUtil.success();
    }

    @GetMapping("/findUserById")
    public ResultVo findUserById(Long id) {
        return ResultUtil.success(userService.findUserById(id));
    }

    @GetMapping("/findUsers")
    public ResultVo findUsers() {
        PageHelper.startPage(0, 5);
        List<User> users = userService.findUsers();
        PageInfo<User> pageInfo = new PageInfo<User>(users);
        return ResultUtil.success(pageInfo);
    }

}
